#pragma once
extern BOOL WriteLog(const char * szFormat, ...);
class CLock
{
public:
	CLock(void){::InitializeCriticalSection(&m_cs);};
	~CLock(void){::DeleteCriticalSection(&m_cs);};
	void Lock(){::EnterCriticalSection(&m_cs);}
	void Unlock(){::LeaveCriticalSection(&m_cs);};
protected:	
	CRITICAL_SECTION m_cs;
};

class CAutoLock
{
public:
	CAutoLock(CLock& lock) :m_lock(lock) 
	{
		m_lock.Lock();

		memset(m_szFile, 0, sizeof(m_szFile));
		m_nLine = 0;
	}
	CAutoLock(CLock& lock, const char *szFile, int nLine) :m_lock(lock) 
	{
		m_lock.Lock();

		strncpy(m_szFile, szFile, sizeof(m_szFile));
		m_nLine = nLine;
		//WriteLog("%s:%d Lock", m_szFile, m_nLine);
	}
	~CAutoLock() 
	{
		m_lock.Unlock();

		if(m_szFile[0] != '\0' && m_nLine > 0)
		{
		    //WriteLog("%s:%d Unlock", m_szFile, m_nLine);
		}
	}
	CLock &m_lock;

	char m_szFile[MAX_PATH];
	int m_nLine;
};